package org.loofer.ext.utils;

import android.content.pm.Signature;
import android.util.Log;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.security.cert.Certificate;
import java.security.cert.CertificateEncodingException;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public final class CertificatesUtils {
    private static final String ANDROID_DEX_FILENAME = "classes.dex";
    private static final String ANDROID_MANIFEST_FILENAME = "AndroidManifest.xml";
    private static final boolean DEBUG_JAR = false;
    public static final String[] IMPORTANT_ENTRY = new String[]{ANDROID_MANIFEST_FILENAME, ANDROID_DEX_FILENAME};
    public static final String[] MANIFEST_ENTRY = new String[]{ANDROID_MANIFEST_FILENAME};
    private static final String TAG = "Certificates";
    private static WeakReference<byte[]> mReadBuffer;
    private static final Object mSync = new Object();

    static class JarFileEnumerator implements Enumeration<JarEntry> {
        private final String[] entryNames;
        private int index = 0;
        private final JarFile jarFile;

        public JarFileEnumerator(JarFile jarFile, String... entryNames) {
            this.jarFile = jarFile;
            this.entryNames = entryNames;
        }

        public boolean hasMoreElements() {
            return this.index < this.entryNames.length;
        }

        public JarEntry nextElement() {
            JarFile jarFile = this.jarFile;
            String[] strArr = this.entryNames;
            int i = this.index;
            this.index = i + 1;
            return jarFile.getJarEntry(strArr[i]);
        }
    }

    private CertificatesUtils() {
    }

    public static Signature[] collectCertificates(String archivePath) {
        return collectCertificates(archivePath, false);
    }

    public static Signature[] collectCertificates(String archivePath, boolean importantOnly) {
        return collectCertificates(archivePath, importantOnly ? IMPORTANT_ENTRY : null);
    }

    public static Signature[] collectCertificates(String archivePath, String... entryNames) {
        if (!isArchiveValid(archivePath)) {
            return null;
        }
        byte[] readBuffer = null;
        synchronized (mSync) {
            WeakReference<byte[]> readBufferRef = mReadBuffer;
            if (readBufferRef != null) {
                mReadBuffer = null;
                readBuffer = (byte[]) readBufferRef.get();
            }
            if (readBuffer == null) {
                readBuffer = new byte[8192];
                readBufferRef = new WeakReference(readBuffer);
            }
        }
        try {
            int i;
            JarFile jarFile = new JarFile(archivePath);
            Certificate[] certs = null;
            Enumeration<JarEntry> entries = createJarEntries(jarFile, entryNames);
            while (entries.hasMoreElements()) {
                JarEntry je = (JarEntry) entries.nextElement();
                if (!(je == null || je.isDirectory() || je.getName().startsWith("META-INF/"))) {
                    Certificate[] localCerts = loadCertificates(jarFile, je, readBuffer);
                    if (localCerts == null) {
                        Log.e(TAG, "File " + archivePath + " has no certificates at entry " + je.getName() + "; ignoring!");
                        jarFile.close();
                        return null;
                    } else if (certs == null) {
                        certs = localCerts;
                    } else {
                        i = 0;
                        while (i < certs.length) {
                            boolean found = false;
                            int j = 0;
                            while (j < localCerts.length) {
                                if (certs[i] != null && certs[i].equals(localCerts[j])) {
                                    found = true;
                                    break;
                                }
                                j++;
                            }
                            if (found && certs.length == localCerts.length) {
                                i++;
                            } else {
                                Log.e(TAG, "File " + archivePath + " has mismatched certificates at entry " + je.getName() + "; ignoring!");
                                jarFile.close();
                                return null;
                            }
                        }
                        continue;
                    }
                }
            }
            jarFile.close();
            synchronized (mSync) {
                mReadBuffer = readBufferRef;
            }
            if (certs != null) {
                if (certs.length > 0) {
                    int N = certs.length;
                    Signature[] signatures = new Signature[certs.length];
                    for (i = 0; i < N; i++) {
                        signatures[i] = new Signature(certs[i].getEncoded());
                    }
                    return signatures;
                }
            }
            Log.e(TAG, "File " + archivePath + " has no certificates; ignoring!");
            return null;
        } catch (CertificateEncodingException e) {
            Log.w(TAG, "Exception reading " + archivePath, e);
            return null;
        } catch (IOException e2) {
            Log.w(TAG, "Exception reading " + archivePath, e2);
            return null;
        } catch (RuntimeException e3) {
            Log.w(TAG, "Exception reading " + archivePath, e3);
            return null;
        }
    }

    private static Certificate[] loadCertificates(JarFile jarFile, JarEntry je, byte[] readBuffer) {
        IOException e;
        RuntimeException e2;
        Throwable th;
        Certificate[] certificateArr = null;
        InputStream is = null;
        try {
            InputStream is2 = new BufferedInputStream(jarFile.getInputStream(je));
            do {
                try {
                } catch (IOException e3) {
                    e = e3;
                    is = is2;
                } catch (RuntimeException e4) {
                    e2 = e4;
                    is = is2;
                } catch (Throwable th2) {
                    th = th2;
                    is = is2;
                }
            } while (is2.read(readBuffer, 0, readBuffer.length) != -1);
            if (je != null) {
                certificateArr = je.getCertificates();
            }
            if (is2 != null) {
                try {
                    is2.close();
                } catch (Exception e5) {
                    Log.w(TAG, "Close IS Exception" + je.getName() + " in " + jarFile.getName(), e5);
                }
            }
            is = is2;
        } catch (IOException e6) {
            e = e6;
            try {
                Log.w(TAG, "Exception reading " + je.getName() + " in " + jarFile.getName(), e);
                if (is != null) {
                    try {
                        is.close();
                    } catch (Exception e52) {
                        Log.w(TAG, "Close IS Exception" + je.getName() + " in " + jarFile.getName(), e52);
                    }
                }
                return certificateArr;
            } catch (Throwable th3) {
                th = th3;
                if (is != null) {
                    try {
                        is.close();
                    } catch (Exception e522) {
                        Log.w(TAG, "Close IS Exception" + je.getName() + " in " + jarFile.getName(), e522);
                    }
                }
                throw th;
            }
        } catch (RuntimeException e7) {
            e2 = e7;
            Log.w(TAG, "Exception reading " + je.getName() + " in " + jarFile.getName(), e2);
            if (is != null) {
                try {
                    is.close();
                } catch (Exception e5222) {
                    Log.w(TAG, "Close IS Exception" + je.getName() + " in " + jarFile.getName(), e5222);
                }
            }
            return certificateArr;
        }
        return certificateArr;
    }

    private static boolean isArchiveValid(String archivePath) {
        if (archivePath == null || archivePath.length() == 0) {
            return false;
        }
        File file = new File(archivePath);
        if (file.exists() && file.isFile()) {
            return true;
        }
        return false;
    }

    private static Enumeration<JarEntry> createJarEntries(JarFile jarFile, String... entryNames) {
        if (entryNames == null || entryNames.length == 0) {
            return jarFile.entries();
        }
        return new JarFileEnumerator(jarFile, entryNames);
    }
}
